package com.ldy.core.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ldy.core.common.Result;
import com.ldy.core.service.BaseSettingService;
import com.ldy.core.vo.SettingVO;
/**
 * 设置
 */
@RestController
@RequestMapping("/setting")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class SettingController {

	@Autowired
	private BaseSettingService settingService;
	
	@Value("${app.gateway:}")
	private String gatewayUrl;
	
	@Value("${app.name:}")
	private String appName;
	
	@PostMapping("/getConfig")
	public Result getConfig(String codes) {
		String[] code = codes.split(",");
		return Result.success(settingService.listByIds(java.util.Arrays.asList(code)));
	}
	
	@PostMapping("/saveConfig")
	public Result saveConfig(@RequestBody SettingVO vo) {
		return Result.success(settingService.saveOrUpdateBatch(vo.getData()));
	}
	
	@PostMapping("/get")
	public Result get(String code) {
		return Result.success(settingService.get(code));
	}
	
	@PostMapping("/save")
	public Result save(String code, String value) {
		Assert.hasText(code, "code  is null");
		settingService.save(code, value);
		return Result.success();
	}
	
	/**
	 * 获取门户地址
	 * @return
	 */
	@PostMapping("/getGatewayUrl")
	public Result getGatewayUrl() {
		return Result.success(this.gatewayUrl);
	}
	
	/**
	 * 获取app名称
	 * @return
	 */
	@PostMapping("/getAppName")
	public Result getAppName() {
		return Result.success(this.appName);
	}
}
